package com.yf.cmw.dao.buss;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.base.common.util.DateUtil;
import com.base.common.util.StringHandler;
import com.yf.cmw.dao.BaseDao;
import com.yf.cmw.entity.MachineLogEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 机器日志表
 * @author chengmingwei
 * @date 2018-08-19 09:09
 */
public class MachineLogDao extends BaseDao {
    public static final String TAB_NAME = "APP_MachineLog";

    public MachineLogDao(Context context) {
        super(context);
    }

    @Override
    protected String getCreateTabSql() {
        StringBuilder sbSql = new StringBuilder();
        sbSql.append("create table ").append(TAB_NAME).append("(")
                .append("_id  INTEGER PRIMARY KEY AUTOINCREMENT,")
                .append("scommand INTEGER,")
                .append("createTime VARCHAR(20),")
                .append("remark VARCHAR(255)")
                .append(")");
        return sbSql.toString();
    }

    @Override
    protected String dropTabSql() {
        return null;
    }

    /**
     *  保存机器日志记录
     * @param obj
     * @return
     */
    public Long save(MachineLogEntity obj){
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("scommand", obj.getScommand());
        values.put("createTime", obj.getCreateTime());
        long id = database.insert(TAB_NAME,null, values);
        obj.setId(id);
        database.close();
        return id;
    }

    /**
     *  删除机器日志
     * @param startDate 开始日期
     * @param endDate   截止日期
     * @return  返回删除的行数
     */
    public int delete(String startDate, String endDate){
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        String selection = null;
        StringBuilder sbWhere = new StringBuilder();
        if(StringHandler.isValidStr(startDate)){
            sbWhere.append(" createTime >= '"+startDate+"' ");
        }

        if(StringHandler.isValidStr(endDate)){
            String sigin = " ";
            if(null != sbWhere && sbWhere.length() > 0){
                sigin = " and ";
            }
            endDate = DateUtil.addDays(endDate,1);
            sbWhere.append(sigin).append(" createTime < '"+endDate+"' ");
        }
        if(null != sbWhere && sbWhere.length() > 0){
            selection = sbWhere.toString();
        }
        int result = database.delete(TAB_NAME, selection, null);
        database.close();
        return result;
    }

    /**
     *  修改机器配置
     * @param obj
     * @return
     */
    public int update(MachineLogEntity obj){
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("scommand", obj.getScommand());
        values.put("remark", obj.getRemark());
        values.put("modifyTime", obj.getModifyTime());
        String whereClause = " _id = ? ";
        String[] whereArgs = new String[]{obj.getId()+""};
        int result = database.update(TAB_NAME,values,whereClause, whereArgs);
        database.close();
        return result;
    }



    /**
     * 获取机器日志列表信息
     * @return
     */
    public List<MachineLogEntity> getList(String startDate, String endDate){
        SQLiteDatabase database = dbHelper.getReadableDatabase();

        String orderBy = " _id desc ";
        String selection = null;
        StringBuilder sbWhere = new StringBuilder();
        if(StringHandler.isValidStr(startDate)){
            sbWhere.append("  createTime >= '"+startDate+"' ");
        }

        if(StringHandler.isValidStr(endDate)){
            String sigin = " ";
            if(null != sbWhere && sbWhere.length() > 0){
                sigin = " and ";
            }
            endDate = DateUtil.addDays(endDate,1);
            sbWhere.append(sigin).append(" createTime < '"+endDate+"' ");
        }
        if(null != sbWhere && sbWhere.length() > 0){
            selection = sbWhere.toString();
        }

        Cursor cursor = database.query(TAB_NAME, null, selection, null, null, null,
                orderBy);
       List<MachineLogEntity> list = new ArrayList<MachineLogEntity>();
        while (cursor.moveToNext()){
            Integer _id = cursor.getInt(cursor.getColumnIndex("_id"));
            Integer scommand = cursor.getInt(cursor.getColumnIndex("scommand"));
            String createTime = cursor.getString(cursor.getColumnIndex("createTime"));
            String modifyTime = cursor.getString(cursor.getColumnIndex("modifyTime"));
            String remark = cursor.getString(cursor.getColumnIndex("remark"));

            MachineLogEntity obj = new MachineLogEntity();
            obj.setId(_id.longValue());
            obj.setScommand(scommand);
            obj.setCreateTime(createTime);
            obj.setModifyTime(modifyTime);
            obj.setRemark(remark);
            list.add(obj);
        }
        cursor.close();
        database.close();
        return list;
    }
}
